package com.fm.pictureselect;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;

import androidx.core.content.ContextCompat;

import com.fm.pictureselect.album.GlideEngine;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.animators.AnimationType;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.luck.picture.lib.style.PictureSelectorUIStyle;
import com.luck.picture.lib.style.PictureWindowAnimationStyle;

import java.util.List;

public class VideoChooseManager {

    public static class Builder {

        private Context context;
        private boolean isWeChatStyle = true;
        private boolean isUseCustomCamera = true;
        private int maxSelectNum = 1;
        private int minSelectNum = 1;
        private int maxVideoSelectNum = 1;
        private int recordVideoSecond = 30;

        private OnChooseVideoListener onChooseVideoListener;

        public Builder setOnChooseVideoListener(OnChooseVideoListener onChooseVideoListener) {
            this.onChooseVideoListener = onChooseVideoListener;
            return this;
        }

        public Builder setRecordVideoSecond(int recordVideoSecond) {
            this.recordVideoSecond = recordVideoSecond;
            return this;
        }

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setWeChatStyle(boolean weChatStyle) {
            isWeChatStyle = weChatStyle;
            return this;
        }


        public Builder setUseCustomCamera(boolean useCustomCamera) {
            isUseCustomCamera = useCustomCamera;
            return this;
        }

        public Builder setMaxSelectNum(int maxSelectNum) {
            this.maxSelectNum = maxSelectNum;
            return this;
        }

        public Builder setMinSelectNum(int minSelectNum) {
            this.minSelectNum = minSelectNum;
            return this;
        }

        public Builder setMaxVideoSelectNum(int maxVideoSelectNum) {
            this.maxVideoSelectNum = maxVideoSelectNum;
            return this;
        }

        public VideoChooseManager build() {
            PictureSelector.create((Activity) context)
                    .openGallery(PictureMimeType.ofVideo())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                    .imageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                    .setPictureUIStyle(PictureSelectorUIStyle.ofNewStyle())
                    .setPictureWindowAnimationStyle(PictureWindowAnimationStyle.ofCustomWindowAnimationStyle(R.anim.picture_anim_up_in, R.anim.picture_anim_down_out))// 自定义相册启动退出动画
                    .isWeChatStyle(isWeChatStyle)// 是否开启微信图片选择风格
                    .isUseCustomCamera(isUseCustomCamera)// 是否使用自定义相机
                    .setLanguage(-1)// 设置语言，默认中文
                    .isPageStrategy(false)// 是否开启分页策略 & 每页多少条；默认开启
                    .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)// 列表动画效果
                    .isWithVideoImage(true)// 图片和视频是否可以同选,只在ofAll模式下有效
                    .isMaxSelectEnabledMask(true)// 选择数到了最大阀值列表是否启用蒙层效果
                    .setCaptureLoadingColor(ContextCompat.getColor(context, R.color.color_2D3340))
                    .maxSelectNum(maxSelectNum)// 最大图片选择数量
                    .minSelectNum(minSelectNum)// 最小选择数量
                    .maxVideoSelectNum(maxVideoSelectNum) // 视频最大选择数量
                    .imageSpanCount(4)// 每行显示个数
                    .isAndroidQTransform(true)// 是否需要处理Android Q 拷贝至应用沙盒的操作，只针对compress(false); && .isEnableCrop(false);有效,默认处理
                    .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)// 设置相册Activity方向，不设置默认使用系统
                    .isOriginalImageControl(true)// 是否显示原图控制按钮，如果设置为true则用户可以自由选择是否使用原图，裁剪功能将会失效
                    .isDisplayOriginalSize(true)// 是否显示原文件大小，isOriginalImageControl true有效
                    .isEditorImage(false)//是否编辑图片
                    .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                    .isSingleDirectReturn(false)// 单选模式下是否直接返回，PictureConfig.SINGLE模式下有效
                    .isPreviewImage(true)// 是否可预览图片
                    .isPreviewVideo(true)// 是否可预览视频
                    .isEnablePreviewAudio(false) // 是否可播放音频
                    .isCamera(true)// 是否显示拍照按钮
                    .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                    .setCameraImageFormat(PictureMimeType.JPEG) // 相机图片格式后缀,默认.jpeg
                    .setCameraVideoFormat(PictureMimeType.MP4)// 相机视频格式后缀,默认.mp4
                    .setCameraAudioFormat(PictureMimeType.AMR)// 录音音频格式后缀,默认.amr
                    .isEnableCrop(false)// 是否裁剪
                    .isCompress(true)// 是否压缩
                    .recordVideoSecond(recordVideoSecond)
                    .synOrAsy(false)//同步true或异步false 压缩 默认同步
                    .isCropDragSmoothToCenter(true)// 裁剪框拖动时图片自动// 跟随居中
                    .isOpenClickSound(true)// 是否开启点击声音
                    .cutOutQuality(90)// 裁剪输出质量 默认100
                    .videoMaxSecond(recordVideoSecond + 1)
                    .minimumCompressSize(100)// 小于多少kb的图片不压缩
                    .forResult(new OnResultCallbackListener<LocalMedia>() {
                        @Override
                        public void onResult(List<LocalMedia> result) {
                            if (onChooseVideoListener != null) {
                                onChooseVideoListener.onResult(result);
                            }
                        }

                        @Override
                        public void onCancel() {
                            if (onChooseVideoListener != null) {
                                onChooseVideoListener.onCancel();
                            }
                        }
                    });
            return new VideoChooseManager();
        }
    }

    public interface OnChooseVideoListener {
        void onResult(List<LocalMedia> result);

        void onCancel();
    }
}
